<template>
  <div>
    <a-card :bordered="false">
      <f-search-form :more="false" @searchHandler="searchHandler">
        <f-search-form-item label="名称" name="linkName"/>
      </f-search-form>
      <f-table ref="table" size="default" rowKey="linkId" :columns="columns" :multiple="false" :operator="operator" :params="queryParam" :api="api.link">
          <span slot="linkLogo" slot-scope="text, record, index">
          <img v-lazy="record.linkLogo" v-viewer class="radius-img">
        </span>
        <span slot="sendStatus" slot-scope="text, record, index">
          <a-switch v-model="record.sendStatus==1" checkedChildren="已发布" unCheckedChildren="未发布" @change="(checked) => setStatus(checked,record)"/>
        </span>
        <span slot="action" slot-scope="text, record">
          <template>
            <a @click="$refs.modal.handleShow(record)">编辑</a>
            <a-divider type="vertical"/>
          </template>
          <a-dropdown>
            <a class="ant-dropdown-link">
              更多 <a-icon type="down"/>
            </a>
            <a-menu slot="overlay">
              <a-menu-item v-if="$auth('table.delete')">
                <a href="javascript:;" @click="$refs.table.remove(record.linkId)">删除</a>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
      </f-table>
      <link-drawer ref="modal"></link-drawer>
    </a-card>
  </div>
</template>

<script>

  import LinkDrawer from './link_drawer'

  export default {
    name: 'TableList',
    components: {
      LinkDrawer

    },
    data () {
      let me = this
      return {
        //列表初始化查询参数
        queryParam: {},
        // 表头
        columns: [
          {
            title: '#',
            width: '60',
            fixed:'left',
            align: 'center',
            customRender: (text, record, index) => index + 1
          },
          {
            title: 'Logo',
            align: 'center',
            scopedSlots: { customRender: 'linkLogo' }
          },
          {
            title: '名称',
            dataIndex: 'linkName'
          },
          {
            title: '链接地址',
            width: 100,
            dataIndex: 'linkUrl',
            customRender: function (text, rerecord) {
              return rerecord.linkPrefix + text
            }
          },
          {
            title: '发布状态',
            align: 'center',
            dataIndex: 'sendStatus',
            scopedSlots: { customRender: 'sendStatus' }
          },
          {
            title: '操作',
            dataIndex: 'action',
            scopedSlots: { customRender: 'action' }
          }
        ],
        //操作按钮
        operator: [
          {
            name: '添加',
            icon: 'plus',
            that: this,
            handler: function (table, that) {
              that.$refs.modal.handleShow()
            }
          },
          {
            name: '删除',
            icon: 'delete',
            batch: true,
            handler: function (table) {
              table.remove()
            }
          }
        ]
      }
    },
    created () {

    },
    methods: {
      setStatus (check, record) {
        this.http.post(this, {
          url: this.api.link.setSendStatus,
          data: {
            ids: record.linkId,
            sendStatus: Number(check)
          },
          success (me, data) {
            record.sendStatus = data
          }
        })
      }
    }
  }
</script>
